package threadpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author sesshomaru
 * @date 2021/10/11 20:34
 */
public class FixedThreadPoolTest {
    public static void main(String[] args) {
        ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(4);
        for (int i = 0; i < 1000; i++) {
            // 通过线程池执行线程任务
            newFixedThreadPool.execute(new Task());
            /**
             * 打印结果始终都是这4个线程
             * pool-1-thread-1
             * pool-1-thread-2
             * pool-1-thread-3
             * pool-1-thread-4
             */
        }
    }
}

class Task implements Runnable{
    @Override
    public void run() {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName());
    }
}
